﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Classes;
using System.Drawing;
using Silvermoon.Controls.Interfaces;
using Silvermoon.Controls.Base;

namespace Silvermoon.Controls
{
    /// <summary>
    /// TreeView shows a hierarchical <see cref="T:TreeViewItem"/> structure in a paged way.
    /// Note that you do not need TreeViewItem as nodes but can also add any derived ItemBase.
    /// </summary>
    public class TreeView : PageContainer, IPage, IInputEventBehavior, IScrollEventBehavior
    {
        #region fields

        private TreeListView listView;
        private string title;

        #endregion
        #region ctor



        public TreeView()
            : base()
        {
            listView = new TreeListView(this, null);
            ItemHeight = Control.ScaleY(48);

            Page = this;
            TextColor = Color.Black;
        }

        #endregion
        #region props

        internal ListViewBase SelectedListView
        {
            get { return Page != null ? Page.Control as ListViewBase : null; }
        }

        public int ItemHeight { get; set; }

        /// <summary>
        /// Gets or sets the title for the root level.
        /// </summary>
        public string Title { get { return title; } set { if (Title != value) { title = value; Invalidate(InvalidationFlag.Text); } } }

        public IList<ItemBase> Items { get { return listView.Items; } }

        public int ItemCount { get { return listView.ItemCount; } set { listView.ItemCount = value; } }

        public ButtonBase Button { get; set; }

        public bool IsRootSelected
        {
            get { return Page == this; }
        }

        #endregion
        #region methods

        public override void OnPageLoaded(IPage oldPage, IPage newPage)
        {
            base.OnPageLoaded(oldPage, newPage);
            TreeViewItem item = newPage as TreeViewItem;
            //if (item != null) item.OnPopulateItems();
            if (item != null) OnPopulate(item);
        }

        public void BeginInit()
        {
            if (this.listView != null) listView.BeginInit();
        }

        public void EndInit()
        {
            if (listView != null) listView.EndInit();
        }

        private TreeViewItemEventArgs treeViewItemEventArgs = new TreeViewItemEventArgs();

        public virtual ItemBase OnAttachItem(int index, TreeViewItem parentItem)
        {
            if (AttachItem != null)
            {
                TreeViewItemEventArgs e = treeViewItemEventArgs;
                e.ParentItem = parentItem;
                e.Index = index;
                AttachItem(this, e);
                TreeViewItem item = e.Item as TreeViewItem;
                if (item != null)
                {
                    item.ListView = parentItem != null ? parentItem.ItemListView : this.listView;
                    item.ParentItem = parentItem;
                }
                return e.Item;
            }
            return null;
        }

        public virtual void OnDetachItem(TreeViewItem item, TreeViewItem parent)
        {
            if (DetachItem != null)
            {
                TreeViewItemEventArgs e = new TreeViewItemEventArgs { ParentItem = parent, Item = item };
                DetachItem(this, e);
            }
        }

        public virtual void OnPopulate(TreeViewItem item)
        {
            if (Populate != null)
            {
                TreeViewEventArgs e = treeViewEventArgs;
                e.Node = item;
                e.NodeCount = item.ItemListView.ItemCount;
                Populate(this, e);
                item.ItemListView.ItemCount = e.NodeCount;
            }
        }

        private TreeViewEventArgs treeViewEventArgs = new TreeViewEventArgs();

        public override void Focus()
        {
            this.listView.Focus();
        }

        #endregion
        #region IPage Members

        IPage IPage.ParentPage
        {
            get { return null; }
        }

        Control IPage.Control { get { return listView; } }
        #endregion
        #region events

        public event EventHandler<TreeViewItemEventArgs> AttachItem;
        public event EventHandler<TreeViewItemEventArgs> DetachItem;

        /// <summary>
        /// Occurs when to populate virtual data for a node.
        /// </summary>
        public event EventHandler<TreeViewEventArgs> Populate;
        #endregion

        #region IInputEventBehavior Members

        public virtual void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (!e.Handled)
            {
                switch (e.KeyCode)
                {
                    case System.Windows.Forms.Keys.Left:
                        e.Handled = true;
                        GoBack();
                        break;
                }
            }
        }

        public virtual void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
        {

        }

        public virtual void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {

        }

        public virtual bool CanFocus { get { return true; } }

        #endregion
    }


}
